#!/bin/bash

# 定义配置文件路径
CONFIG_FILE="./chunk_io.toml"

# 定义要测试的fingerprint选项列表
FINGERPRINTS=(
    "sha1"
    "mh_sha1"
    "mh_sha1_sse"
    "mh_sha1_avx"
    "mh_sha1_avx2"
    "mh_sha1_avx512"
    "sha256"
    "mh_sha256"
    "mh_sha256_sse"
    "mh_sha256_avx"
    "mh_sha256_avx2"
    "mh_sha256_avx512"
)

# 备份原始配置文件
cp "$CONFIG_FILE" "${CONFIG_FILE}.bak"

# 遍历所有fingerprint选项
for fp in "${FINGERPRINTS[@]}"; do
    echo "============================================"
    echo "Testing with fingerprint: $fp"
    echo "============================================"
    
    # 更新配置文件中的fingerprint选项
    # 先注释掉所有fingerprint行
    sed -i '/^fingerprint = /s/^/# /' "$CONFIG_FILE"
    # 取消当前fingerprint的注释或添加新行
    if grep -q "# fingerprint = \"${fp}\"" "$CONFIG_FILE"; then
        sed -i "s/^# fingerprint = \"${fp}\"/fingerprint = \"${fp}\"/" "$CONFIG_FILE"
    else
        echo "fingerprint = \"${fp}\"" >> "$CONFIG_FILE"
    fi
    
    # 显示修改后的fingerprint行确认
    grep -E "^fingerprint = |^# fingerprint = " "$CONFIG_FILE"

    # 运行测试命令
    echo "Running tests..."
    
    # 执行测试并记录完整输出
    cargo test --release -p chunk-io2 -- --nocapture -- backup::test_two_method > /dev/null 2>&1
    
    # 检查命令执行状态并提取关键信息
    if [ $? -eq 0 ]; then
        echo "✅ Test PASSED with fingerprint: $fp"
        # 显示测试统计信息
        tail -n 5 "$LOG_FILE" | grep -E "test result|passed|failed"
    else
        echo "❌ Test FAILED with fingerprint: $fp"
        # 显示错误摘要
        echo "=== Error Summary ==="
        grep -E "error:|failures:|FAILED|test result:" "$LOG_FILE"
        echo "=== Last 10 lines ==="
        tail -n 10 "$LOG_FILE"
    fi
done

# 恢复原始配置文件
mv "${CONFIG_FILE}.bak" "$CONFIG_FILE"
echo "Configuration file restored to original state."
echo "All test logs saved in ${LOG_DIR}/ directory"